package com.leetcode.problems003;

import java.util.HashSet;
import java.util.Set;

/**
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() <= 1){
            return s.length();
        }
        Set<Character> exists = new HashSet<>();
        int size = s.length();
        int startIndex = 0;
        int i = 1;
        int maxLen = 1;
        exists.add(s.charAt(0));
        while(i < size){
            //当前查看的字符
            char currentChar = s.charAt(i);
            if(!exists.contains(currentChar)){
                exists.add(currentChar);
                i++;
                continue;
            }
            maxLen = Math.max(i - startIndex,maxLen);
            exists.remove(s.charAt(startIndex));
            startIndex++;
        }
        maxLen = Math.max(i - startIndex,maxLen);
        return maxLen;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().lengthOfLongestSubstring("abcdef"));
    }
}